﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Text;
using System.Collections.ObjectModel;

namespace Sitecore.Models
{
    public class Book : IAssignable
    {
        [Required]
        [StringLength(50, MinimumLength = 1)]
        public string Title { get; set; }

        [Required]
        [Range(0, 2050, ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "YearErrorMessage")]
        public int Year { get; set; }

        [Required]
        [StringLength(4000)]
        public string Description { get; set; }

        public string Authors
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                if (_Authors != null)
                {
                    foreach (Author a in _Authors)
                    {
                        sb.Append(a.LastName);
                        sb.Append(" ");
                        sb.Append(a.FirstName);
                        sb.Append(", ");
                    }
                }
                return sb.ToString().TrimEnd(", ".ToCharArray());
            }
        }

        public List<Author> _Authors { get; set; }

        public Client Client { get; set; }

        public int ID { get; set; }


        public void AssignTo(object domain)
        {
            if (domain is DataProject.Book)
            {
                DataProject.Book book = domain as DataProject.Book;
                book.Description = this.Description;
                book.Title = this.Title;
                book.Year = this.Year;
                book.Id = this.ID;
                book.Period = null;

                if (this.Client != null)
                {
                    book.Client = new System.Data.Objects.DataClasses.EntityCollection<DataProject.Client>();
                    DataProject.Client domainClient = new DataProject.Client();
                    Client.AssignTo(domainClient);
                    book.Client.Add(domainClient);
                }

                if (this._Authors != null)
                {
                    book.Author = new System.Data.Objects.DataClasses.EntityCollection<DataProject.Author>();
                    foreach (Author a in _Authors)
                    {
                        DataProject.Author domainAuthor = new DataProject.Author();
                        a.AssignTo(domainAuthor);
                        book.Author.Add(domainAuthor);
                    }
                }
            }
            else throw new NotSupportedException("not valid domain type");
        }

        public static Book FromDbModel(DataProject.Book domainBook)
        {
            Book book = new Book();
            book.Description = domainBook.Description;
            book.Title = domainBook.Title;
            book.Year = domainBook.Year;
            book.ID = domainBook.Id;
            if (domainBook.Author.Count != 0)
            {
                book._Authors = new List<Author>();
                foreach (DataProject.Author domainAuthor in domainBook.Author)
                {
                    book._Authors.Add(Author.FromDbModel(domainAuthor));
                }
            }
            if (domainBook.Client.Count != 0)
            {
                book.Client = new Client();
                book.Client = Client.FromDbModel(domainBook.Client.Last());
            }
            return book;
        }
    }
}